package com.qf.action;

import com.qf.common.SGHelper;
import com.qf.pojo.vo.User;
import com.qf.utils.CustomException;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @创建人: fengli
 * @创建时间: 2018/11/8 0008 16:56
 * @包: com.qf.action
 * @描述:
 */
@Controller
@RequestMapping("/app")
public class UserAction {
    @ResponseBody
    @RequestMapping(value = "/login")
    public JSONObject login(HttpServletRequest request, User user) {//：用户登录接口
        try{
            return SGHelper.login(user);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getVerificationCode")
    public JSONObject getVerificationCode(HttpServletRequest request, String mailBox) {//注册时获取验证码接口
        try {
            return SGHelper.getVerificationCode(mailBox);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/userRegister")//注册
    public JSONObject userRegister(HttpServletRequest request, User user) {//注册接口-注册成功后返回用户ID
        try {
            return SGHelper.userRegister(user);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/resetPasswords")
    public JSONObject resetPasswords(HttpServletRequest request, String testName) {//重置密码时获取邮箱验证码
        try {
            return SGHelper.resetPasswords(testName);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/modificationUserPassWord")
    public JSONObject modificationUserPassWord(HttpServletRequest request, User user) {// 重置密码接口
        try {
            return SGHelper.modificationUserPassWord(user);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getTenantFromUser")
    public JSONObject getTenantFromUser(HttpServletRequest request, String testerId) {//根据用户登录信息,查询用户下的所有租户
        try {
            return SGHelper.getTenantFromUser(testerId);
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getAllProductsNameId")
    public JSONObject getAllProductsNameId(HttpServletRequest request, HttpServletResponse response, String tenantID) {//根据所选租户id,查询该租户下的所有产品
        try {
            return JSONObject.fromObject(SGHelper.getAllProductsNameId(tenantID));
        } catch (CustomException e) {
            return JSONObject.fromObject("{\"code\":\"-2000\",\"msg\":\"" + e.getMessage() + "\"}");
        }
    }
}
